package org.ww.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.ww.edu.bean.Course;
import org.ww.edu.bean.CourseDescription;
import org.ww.edu.bean.frontvo.CourseWebVo;
import org.ww.edu.bean.frontvo.FrontCourseVo;
import org.ww.edu.bean.vo.CourseInfoVo;
import org.ww.edu.bean.vo.CoursePublishVo;
import org.ww.edu.bean.vo.CourseQuery;
import org.ww.edu.mapper.CourseMapper;
import org.ww.edu.service.ChapterService;
import org.ww.edu.service.CourseDescriptionService;
import org.ww.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ww.edu.service.VideoService;
import org.ww.servicebase.exceptionhandler.GuliException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-10-23
 */
@Service
@AllArgsConstructor
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    private CourseDescriptionService courseDescriptionService;
    private VideoService videoService;
    private ChapterService chapterService;
    //添加课程基本信息
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        //创建课程对象
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int index = baseMapper.insert(course);
        if(index<=0){
            throw new GuliException(20001,"添加课程基本信息失败");
        }

        //获取课程id
        String cid = course.getId();

        //添加课程简介
        //创建课程简介对象
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置课程简介id需要和课程id一致
        courseDescription.setId(cid);
        //添加到数据库中
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询id查询课程基本信息
        Course course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        System.out.println("测试"+course);
        BeanUtils.copyProperties(course,courseInfoVo);
        //根据id查询课程描述基本信息
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    //根据课程id修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1 根据课程id修改课程信息
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int index = baseMapper.updateById(course);
        if(index==0){
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2 根据课程id修改课程描述信息
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        CoursePublishVo coursePublishInfo = baseMapper.getCoursePublishInfo(courseId);
        return coursePublishInfo;
    }

    /**
     * 多条件查询课程列表
     * @param page
     * @param limit
     * @param courseQuery
     * @return
     */
    @Override
    public IPage<Course> pageCourse(long page, long limit, CourseQuery courseQuery) {
        Page<Course> p=new Page<>(page,limit);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        //取出其中的参数
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        return baseMapper.selectPage(p,wrapper);
    }
    //根据课程id删除课程信息
    //依次删除：视频，小节，章节，课程描述，课程
    @Override
    public void deleteCourse(String courseId) {
        //1 根据课程id删除小节
        videoService.deleteByCourseId(courseId);
        //2 根据课程id删除章节
        chapterService.deleteByCourseId(courseId);
        //3 根据课程id删除课程描述
        courseDescriptionService.removeById(courseId);
        //4 根据课程id删除课程
        int index = baseMapper.deleteById(courseId);
        if(index==0){
            throw new GuliException(20001,"删除课程信息失败");
        }
    }

    /**
     * 查询前8条热门课程
     * @return
     */
    @Override
    @Cacheable(value = "course",key = "'selectHotCourse'")
    public List<Course> getHotCourse() {
        QueryWrapper<Course> courseWrapper=new QueryWrapper<>();
        courseWrapper.orderByDesc("id");
        courseWrapper.last("limit 8");
        List<Course> courseList = baseMapper.selectList(courseWrapper);
        return courseList;
    }

    /**
     * 多条件带分页查询前端课程信息
     * @param pageParam
     * @param courseVo
     * @return
     */
    @Override
    public Map<String, Object> pageCourseInfo(Page<Course> pageParam, FrontCourseVo courseVo) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseVo.getSubjectId())){
            wrapper.eq("subject_id",courseVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<Course> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    /**
     * 根据课程id查询课程详细信息
     * @param courseId
     * @return
     */
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
